package com.example.siyun.controller.productcategroycontroller;

import com.example.siyun.pojo.JsProductCategroy;
import com.example.siyun.pojo.TreeNodes;
import com.example.siyun.service.productcategroyservice.ProductCategroyService;
import com.example.siyun.util.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/Cate")
public class ProductCategroyController {
   @Autowired
    private ProductCategroyService productCategroyService;


    /**
     * 树的展示
     * @return
     */
    @RequestMapping("/advanced/getpid")
    public List<TreeNodes> getTree() {
        return productCategroyService.getpid(0);
    }

    /**
     * 树的修改
     * @param treeNodes
     * @return
     */
    @RequestMapping("/advanced/edit")
    public PageResult editTree(TreeNodes treeNodes) {
        System.out.println(treeNodes);
        PageResult js = new PageResult();
        log.info("修改信息");
        try {
            productCategroyService.updateTreeName(treeNodes);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("修改信息成功");
        return js;
    }

    /**
     * 树的删除
     * @param categrayId
     * @return
     */
    @RequestMapping("/advanced/del")
    public PageResult delTree(Integer categrayId) {

        System.out.println(categrayId);
        PageResult js = new PageResult();
        log.info("删除信息");
        try {
            productCategroyService.delTreeNodes(categrayId);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("删除信息成功");
        return js;
    }

    /**
     * 树的添加
     * @param jsProduct
     * @return
     */
    @RequestMapping("/advanced/addTree")
    public PageResult addTree(JsProductCategroy jsProduct) {
        PageResult js = new PageResult();
        Map<String, String> map = new HashMap<>();
        log.info("添加信息");
        try {
            productCategroyService.addTreeNodes(jsProduct);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("添加信息成功");
        return js;
    }
}
